package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.vo.Movements;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    /**
     * 发布动态
     * @param textContent
     * @param location
     * @param longitude
     * @param latitude
     * @param imageContent
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveMovements(@RequestParam("textContent")String textContent,
                                              @RequestParam("location")String location,
                                              @RequestParam("longitude")String longitude,
                                              @RequestParam("latitude")String latitude,
                                              @RequestParam("imageContent")MultipartFile[] imageContent){

        try {
            Boolean bool= this.movementsService.saveMovements(textContent,location,longitude,latitude,imageContent);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询好友的动态信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryPublishList(@RequestParam("page")Integer page,
                                                       @RequestParam("pagesize")Integer pageSize){
        try {
            PageResult pageResult=this.movementsService.queryUserPublishList(page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询推荐的动态信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult> queryRecommendPublishList(@RequestParam("page")Integer page,
                                                       @RequestParam("pagesize")Integer pageSize){
        try {
            PageResult pageResult=this.movementsService.queryRecommendPublishList(page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 给动态点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId){
        Long count=this.movementsService.likeComment(publishId);
        if (null!=count){
            return ResponseEntity.ok(count);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消动态点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> dislikeComment(@PathVariable("id") String publishId){
        Long count=this.movementsService.dislikeComment(publishId);
        if (null!=count){
            return ResponseEntity.ok(count);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 给动态点喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id") String publishId){
        Long count=this.movementsService.loveComment(publishId);
        if (null!=count){
            return ResponseEntity.ok(count);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消动态的喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> unloveComment(@PathVariable("id") String publishId){
        Long count=this.movementsService.unloveComment(publishId);
        if (null!=count){
            return ResponseEntity.ok(count);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     *  查询单挑动态信息
     * @param publishId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Movements> queryMovementsById(@PathVariable("id") String publishId){
        Movements movements=this.movementsService.queryMovementsById(publishId);
        try {
            if (null!=movements){
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
